package com.huluobo.a2204parkdemo.notice

import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2204parkdemo.R
import com.huluobo.a2204parkdemo.adapter.NoticeAdapter
import com.huluobo.a2204parkdemo.databinding.ActivityNoticeBinding
import com.huluobo.a2204parkdemo.pagingAdapter.NoticePagingAdapter
import com.huluobo.base.bean.NoticeBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NoticeActivity : BaseActivity<ActivityNoticeBinding, NoticeViewModel>() {
    //    private lateinit var noticeAdapter: NoticeAdapter
    private lateinit var noticePagingAdapter: NoticePagingAdapter
    override fun initData() {

//        viewModel.getAllNotice(getNotice())
//        viewModel.getAllNoticeSuccess.observe(this) {
//            val list = Gson().fromJson(it, NoticeBean::class.java)
//            noticeAdapter.data.clear()
//            noticeAdapter.data.addAll(list)
//            noticeAdapter.notifyDataSetChanged()
//        }

        viewModel.addNoticeSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            viewModel.getAllNotice(getNotice())
        }
    }

    private fun getNotice(): RequestBody {
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_USER_ID] = SPUtils.getInstance().getString(Const.PARAM_USER_ID)
        val json = Gson().toJson(map)
        return RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
    }

    override fun initView() {
        binding.noticeHeader.headerTitle.text = "公告管理"
        binding.noticeHeader.headerBack.setOnClickListener { finish() }
        binding.noticeHeader.headerAdd.visibility = View.VISIBLE
        binding.noticeHeader.headerAdd.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_notice, null)
            AlertDialog.Builder(this)
                .setView(view)
                .setTitle("添加新公告")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定") { dialog, which ->
                    val title = view.findViewById<EditText>(R.id.dialog_notice_title_et).text.toString()
                    val value = view.findViewById<EditText>(R.id.dialog_notice_value_et).text.toString()
                    if (title.isNotEmpty() && value.isNotEmpty()) {
                        val map = mutableMapOf<String, String>()
                        map[Const.PARAM_NOTICE_TITLE] = title
                        map[Const.PARAM_NOTICE_VALUE] = value
                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        viewModel.addNotice(body)
                    } else {
                        ToastUtils.showLong("公告标题或者内容不能为空")
                    }

                }.show()
        }
        binding.noticeRv.layoutManager = LinearLayoutManager(this)
        noticePagingAdapter = NoticePagingAdapter()
        binding.noticeRv.adapter = noticePagingAdapter

        lifecycleScope.launch {
            viewModel.getNewPagingData().collect {
                noticePagingAdapter.submitData(it)
            }
        }
    }
}